import { init } from '@rematch/core'

interface State {
  activeNav:number,//导航选中下标
  isLogin:boolean,//用户是否登录
  userInfo:any,//用户信息
  playList:any[],//播放列表（名称，歌手）
  mp3List:any[]//播放音乐的列表(歌曲Url)
}
//开始使用rematch 大干一场吧
let store = {
  state: {
    // loaderOk: false,//是否加载成功
    activeNav:0,//导航选中下标
    isLogin:false,//用户是否登录
    userInfo:null,//用户信息
    playList:[],//播放列表（名称，歌手）
    mp3List:[],//播放音乐的列表(歌曲Url)
  },
  //类似vuex 修改数据的那个函数
  reducers: {
    // changeLoaderTypeFun(state, payload){
    //   const newState= JSON.parse(JSON.stringify(state));
    //   newState.loaderOk=payload;
    //   return newState;
    // },
    // setLocalCitysFun(state, payload){
    //   const newState= JSON.parse(JSON.stringify(state));
    //   newState.localCitys=payload;
    //   return newState;
    // }
    navClickFun(state:State, index:number){
      const newState= JSON.parse(JSON.stringify(state));
      newState.activeNav=index;
      return newState;
    },
    outSizeFun(state:State,obj:any){
      const newState= JSON.parse(JSON.stringify(state));
      newState.price=obj.price;
      newState.count=obj.count;
      return newState;
    },
    //设置用户信息
    setUserInfoFun(state:State,userInfo:any){
      const newState= JSON.parse(JSON.stringify(state));
      newState.userInfo=userInfo.userInfo;
      newState.isLogin=userInfo.isLogin;
      return newState;
    },
    //存播放列表
    setPlayListFun(state:State,playList:any){
      const newState= JSON.parse(JSON.stringify(state));
      newState.playList=playList;
      return newState;
    },
    //存音乐播放url的数据
    setMp3ListFun(state:State,mp3List:any){
      const newState= JSON.parse(JSON.stringify(state));
      newState.mp3List=mp3List;
      return newState;
    },
  },
  //类似vuex 的action
  effects: {
    // navClick(index:number, state:any){
    //   (this as any).navClickFun(index)
    // },
    navClickAct(index:number, state:State){
      (this as any).navClickFun(index)
    },
    setUserInfoAct(userInfo:any, state:State){
      (this as any).setUserInfoFun(userInfo)
    },
    setPlayListAct(playList:any, state:State){
      (this as any).setPlayListFun(playList)
    },
    setMp3ListAct(mp3List:any, state:State){
      (this as any).setMp3ListFun(mp3List)
    },
  }
}
let returnStore = init(({
  models:{
    store
  }
} as any))
export default returnStore;